/**
 * Copyright (c) 2006 Amgad S Youssef
 * All Rights Reserved.
 * Licensed under the Eclipse Public License - v 1.0
 * For more information see http://www.eclipse.org/legal/epl-v10.html
 */
package org.eclipsejcde.wizards.jcapplet;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipsejcde.wizards.jcapplet.pages.NewJCAppletExtraFeaturesWizardPage;
import org.eclipsejcde.wizards.jcapplet.pages.NewJCAppletWizardPage;

/**
 * @author Amgad S Youssef
 *
 */
public class NewJCAppletWizard extends Wizard implements INewWizard {
	protected IStructuredSelection selection;

	protected NewJCAppletWizardPage newJCAppletWizardPage;

	protected NewJCAppletExtraFeaturesWizardPage newJCAppletExtraFeaturesWizardPage;

	public NewJCAppletWizard() {
		super();
		setNeedsProgressMonitor(true);
		setWindowTitle("New Java Card Applet");
	}

	@Override
	public boolean performFinish() {
		IRunnableWithProgress createJCAppletOperation = new IRunnableWithProgress() {
			@Override
			public void run(IProgressMonitor monitor) throws InvocationTargetException {
				try {
					doCreateJCApplet(monitor);
				} catch (CoreException e) {
					throw new InvocationTargetException(e);
				} catch (InterruptedException e) {
					throw new InvocationTargetException(e);
				} catch (Exception e) {
					e.printStackTrace();
					throw new InvocationTargetException(e);
				} finally {
					monitor.done();
				}
			}
		};
		IRunnableWithProgress createExtraFeaturesOperation = new IRunnableWithProgress() {
			@Override
			public void run(IProgressMonitor monitor) throws InvocationTargetException {
				try {
					doCreateExtraFeatures(monitor);
				} catch (CoreException e) {
					throw new InvocationTargetException(e);
				} catch (InterruptedException e) {
					throw new InvocationTargetException(e);
				} catch (Exception e) {
					e.printStackTrace();
					throw new InvocationTargetException(e);
				} finally {
					monitor.done();
				}
			}
		};
		try {
			getContainer().run(true, false, createJCAppletOperation);
			getContainer().run(true, false, createExtraFeaturesOperation);
		} catch (InterruptedException e) {
			return false;
		} catch (InvocationTargetException e) {
			e.printStackTrace();
			Throwable realException = e.getTargetException();
			MessageDialog.openError(getShell(), "Error", realException.getMessage());
			return false;
		}
		return true;
	}

	public void doCreateJCApplet(IProgressMonitor monitor) throws CoreException, InterruptedException {
		monitor.beginTask("Creating Java Card Applet", 1);
		newJCAppletWizardPage.createType(monitor);
		monitor.worked(1);
	}

	public void doCreateExtraFeatures(IProgressMonitor monitor) throws CoreException, InterruptedException {
		monitor.beginTask("Creating Extra Features", 4);

		monitor.setTaskName("creating remote interface");
		newJCAppletExtraFeaturesWizardPage.createRIType(monitor);
		monitor.worked(1);

		monitor.setTaskName("creating remote interface implementer");
		newJCAppletExtraFeaturesWizardPage.createRIImplType(monitor);
		monitor.worked(2);

		monitor.setTaskName("creating shareable interface");
		newJCAppletExtraFeaturesWizardPage.createSIType(monitor);
		monitor.worked(3);

		monitor.setTaskName("creating shareable interface implementer");
		newJCAppletExtraFeaturesWizardPage.createSIImplType(monitor);
		monitor.worked(4);
	}

	@Override
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		this.selection = selection;
		newJCAppletWizardPage = new NewJCAppletWizardPage();
		newJCAppletWizardPage.init(this.selection);
		newJCAppletExtraFeaturesWizardPage = new NewJCAppletExtraFeaturesWizardPage(newJCAppletWizardPage);
	}

	@Override
	public void addPages() {
		addPage(newJCAppletWizardPage);
		addPage(newJCAppletExtraFeaturesWizardPage);
	}

	@Override
	public boolean performCancel() {
		return super.performCancel();
	}

}
